package com.lebang.controller.ybdapi;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Require;
import com.lebang.component.RedisClient;
import com.lebang.entity.RequestHeaders;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.PointService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @Author: hwq
 * @Date: 2021/1/10 14:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/ybd/main")
public class ybdApi {
    private BigDecimal mi = new BigDecimal(0.5);
    private BigDecimal kaluli = new BigDecimal(0.05);
    private BigDecimal time = new BigDecimal(0.45);
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private PointService pointService;

    /**
     * 根据步数获取参数
     *
     * @param request
     * @param step    步数
     * @return
     */
    @PostMapping("/bili")
    public JsonData bli(HttpServletRequest request, BigDecimal step) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mi", step.multiply(mi).setScale(2,BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
        jsonObject.put("kaluli", step.multiply(kaluli).setScale(2,BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
        jsonObject.put("time",step.multiply(time).setScale(2,BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
        return Callback.success("success", "查询成功", jsonObject);
    }
    /**
     * 根据步数获取参数
     *
     * @param request
     * @param step    步数
     * @return
     */
    @PostMapping("/lajifu")

    public JsonData bli(HttpServletRequest request, BigDecimal height,BigDecimal weight) {
        height=height.divide(new BigDecimal(100));
        height=height.multiply(height);
        BigDecimal divide = weight.divide(height,2,BigDecimal.ROUND_DOWN);
        String s = (String) divide. toPlainString();
        return Callback.success("success", "查询成功",s);
    }
    /**
     * 根据步数获取参数
     *
     * @param request
     * @param step    步数
     * @return
     */
    @PostMapping("/fangshao")
    @Require
    public JsonData fangshao(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mubiao", 7000);
        jsonObject.put("jinbi", 3000);
        jsonObject.put("is", redisClient.hexists("MAIDAN"+ LocalDate.now().format(DateTimeFormatter.ISO_DATE),requestHeaders.getUserid()));
        return Callback.success("success", "查询成功",jsonObject);
    }

    /**
     * 根据步数获取参数
     *
     * @param request
     * @param step    步数
     * @return
     */
    @PostMapping("/maidan")
    @Require
    public JsonData maidan(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        if(redisClient.hexists("MAIDAN"+ LocalDate.now().format(DateTimeFormatter.ISO_DATE),requestHeaders.getUserid())){
            return Callback.fail("200", "已领奖");
        }else {
            redisClient.hset("MAIDAN"+ LocalDate.now().format(DateTimeFormatter.ISO_DATE),requestHeaders.getUserid(),"1");
            pointService.addPoint(requestHeaders.getUserid(),3000, TaskCategory.MUBIAODACHEN,requestHeaders,"达成步数目标奖励");
            return Callback.success("200", "领奖成功");
        }

    }


    @PostMapping("/title")
    public JsonData title(HttpServletRequest request) {



        return Callback.success("success", "查询成功", "5.【温馨贴士】根据不同范围值进行不同提示\n" +
                "\n" +
                "偏差（18.5以下）：能量摄入相对不足，易导致营养不良等\n" +
                "正常（18.5 ~ 23.9）：指标十分正常，请继续保持\n" +
                "偏胖（24.0 ~ 27.9）：\n" +
                "能量摄入相对过多或活动不足，易导致超重和肥胖，可显著增加2型糖尿病、冠心病及结肠癌等疾病的发生风险。\n" +
                "肥胖（28含以上）：\n" +
                "要想纠正肥胖，首先要从饮食做起，在饮食上要注意，以清淡饮食摄入，不要食用高热量，高脂肪，高油腻的食物，这样可是会长胖的。\n" +
                "用餐也要注意，不要过饱最好以八分饱为宜，就餐之后不要立即坐下，要做适量的运动，这样有助于消食。");
    }
}
